package amdin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class A_addc extends JPanel implements ActionListener{
    JTextField 课程号,课程名;
    JButton 录入;
    
    public A_addc(){
        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
        catch(Exception e){System.err.println("不能设置外观: "+e);}
        课程号=new JTextField(20);
        课程名=new JTextField(20);
        课程号.setBounds(400,50,280,40);
        课程名.setBounds(400,100,280,40);
        this.JTextField(new GridLayout(3,2));

        录入=new JButton("添加");
        录入.addActionListener(this);
        Box box1=Box.createHorizontalBox();//横放box
        //box1.getHeight();
        //box1.getBorder();
        Box box2=Box.createHorizontalBox();
        Box box3=Box.createHorizontalBox();
        Box box4=Box.createHorizontalBox();
        box1.add(new JLabel("课程号:"));
        box1.add(课程号);
        box2.add(new JLabel("课程名:"));
        box2.add(课程名);
        box4.add(录入);
        Box boxH=Box.createVerticalBox();//竖放box
        boxH.add(box1);
        boxH.add(box2);
        boxH.add(box3);
        boxH.add(box4);
        boxH.add(Box.createVerticalGlue());
        JPanel messPanel=new JPanel();
        messPanel.add(boxH);
        setLayout(new BorderLayout());
        add(messPanel,BorderLayout.CENTER);
        validate();
    }

    private void JTextField(GridLayout gridLayout) {
        setBackground(Color.green);
    }

    public void actionPerformed(ActionEvent c){
        Object obj=c.getSource();
        if(obj==录入){
            if(课程号.getText().equals("")||课程名.getText().equals("")){
                JOptionPane.showMessageDialog(this,"学生信息请填满再录入！ " );
            }
            Statement stmt=null;
            ResultSet rs=null,rs1=null;
            String sql,sql1;
            sql1="select * from kecheng where kecheng_id='"+课程号.getText()+"'";
            sql="insert into kecheng values('"+课程号.getText()+"','"+课程名.getText()+"')";
            try{
                Connection dbConn1=CONN();
                stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                rs1=stmt.executeQuery(sql1);
                if(rs1.next()){JOptionPane.showMessageDialog(this,"该课号以存在，无法添加哦 ");}
else{
                        stmt.executeUpdate(sql);
                        JOptionPane.showMessageDialog(this,"添加成功咯");
                    }
                    rs1.close();
                    stmt.close();
                }
catch(SQLException e){
                    System.out.print("SQL Exception occur.Message is:"+e.getMessage());
                }
            }
        }
//连接数据库方法
        public static Connection CONN(){
            String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
            String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=CourseManageSystem"; //连接服务器和数据库test
            String userName = "sa"; //默认用户名
            String userPwd = "123456"; //密码
            Connection dbConn=null;
            try {
                Class.forName(driverName);
                dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
                System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
            } catch (Exception e) {
                e.printStackTrace();
            }
            return dbConn;
        }
    }
